package com.eda;

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;

public class PubActivity extends TabActivity {
	
	private TabWidget mTabWidget;
	private TextView titleText;
	private TabHost tabHost;
	private Button backBut;
	private Button doBut;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.activity_pub);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.activity_common_title_bar);
		
		Bundle bundleData = getIntent().getExtras();
		titleText = (TextView)findViewById(R.id.commonTitle);
		if(bundleData.getInt("targetTab")==1){
			titleText.setText(PubActivity.this.getString(R.string.pub_pub_title));
		}else{
			titleText.setText(PubActivity.this.getString(R.string.pub_find_title));
		}
		
		
		backBut = (Button)findViewById(R.id.commonBack);
		backBut.setOnClickListener(goHome);
		doBut = (Button)findViewById(R.id.commonFunction);
		doBut.setOnClickListener(commonSearch);
		doBut.setVisibility(-1);
		
		/* 去除标签下方的白线 */
		tabHost = getTabHost();
		tabHost.setPadding(tabHost.getPaddingLeft(),
                tabHost.getPaddingTop(), tabHost.getPaddingRight(),
                tabHost.getPaddingBottom() - 5);

		Intent layout1intent = new Intent();
        layout1intent.setClass(this, PubFindActivity.class);
        TabHost.TabSpec layout1spec = tabHost.newTabSpec("查找车源");
        layout1spec.setIndicator("查找车源");
        layout1spec.setContent(layout1intent);
        tabHost.addTab(layout1spec);

        Intent layout2intent = new Intent();
        layout2intent.setClass(this, PubPublishActivity.class);
        TabHost.TabSpec layout2spec = tabHost.newTabSpec("发布货源");
        layout2spec.setIndicator("发布货源");
        layout2spec.setContent(layout2intent);
        tabHost.addTab(layout2spec);

        Intent layout3intent = new Intent();
        layout3intent.setClass(this, PubMycargoActivity.class);
        TabHost.TabSpec layout3spec = tabHost.newTabSpec("我的货源");
        layout3spec.setIndicator("我的货源");
        layout3spec.setContent(layout3intent);
        tabHost.addTab(layout3spec);
		
		tabHost.setCurrentTab(bundleData.getInt("targetTab"));
		tabHost.setOnTabChangedListener(tabChangeListener);
		mTabWidget = tabHost.getTabWidget();
		
		for (int i = 0; i < mTabWidget.getChildCount(); i++)
        {
            /* 得到每个标签的视图 */
            View view = mTabWidget.getChildAt(i);
            /* 设置每个标签的背景 */
            
            /* 设置Tab间分割竖线的颜色 */
            // tabWidget.setBackgroundColor(Color.WHITE);
            /* 设置Tab间分割竖线的背景图片 */
            // tabWidget.setBackgroundResource(R.drawable.icon);
            /* 设置tab的高度 */
            mTabWidget.getChildAt(i).getLayoutParams().height = 50;
            TextView tv = (TextView) mTabWidget.getChildAt(i).findViewById(
                    android.R.id.title);
            /* 设置tab内字体的颜色 */
            tv.setTextColor(Color.rgb(49, 116, 171));
        }
		
	}
	
	public void setOnTabChangedListener (TabHost.OnTabChangeListener l){
		
	}
	
	OnTabChangeListener tabChangeListener = new OnTabChangeListener() {
		
		@Override
		public void onTabChanged(String arg0) {
			if(tabHost.getCurrentTab()==0){
				titleText.setText(PubActivity.this.getString(R.string.pub_find_title));
				doBut.setVisibility(0);
				doBut.setText("搜索");
			}else if(tabHost.getCurrentTab()==1){
				titleText.setText(PubActivity.this.getString(R.string.pub_pub_title));
				doBut.setVisibility(-1);
			}else if(tabHost.getCurrentTab()==2){
				titleText.setText(PubActivity.this.getString(R.string.pub_mycargo_title));
				doBut.setVisibility(0);
				doBut.setText("搜索");
				/*搜索 我的货源*/
				//
			}
				
		}
	};
	
	private OnClickListener goHome = new OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
			Intent goHomeIntent = new Intent(PubActivity.this,
					MainActivity.class);
			startActivity(goHomeIntent);
		}
	};
	
	private OnClickListener commonSearch = new OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			
		}
	}; 
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.pub, menu);
		return true;
	}
}
